/**
 * 
 */
package com.rytong.tools.ui;

import java.io.InputStream;
import java.util.Vector;

import com.rytong.tools.utils.Utils;



import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

/**
 * @author Chris
 * 
 */
public class ImageRepository {

    private Bitmap arrSelect_;

    private Bitmap statusSelected_;

    private Bitmap backgroundTab_;

    private Bitmap arrTab_;

    /**
     * 
     */
    public ImageRepository() {
    }

    public static Bitmap decodeBitmap(Resources rs, int resId) {
        Bitmap bitmap = null;
        BitmapFactory.Options opts = new BitmapFactory.Options();
        try {
            BitmapFactory.Options.class.getField("inPurgeable").set(opts, true);
        } catch (Exception e) {
        }
        try {
            bitmap = BitmapFactory.decodeResource(rs, resId, opts);
        } catch (Exception e) {
        }
        return bitmap;
    }

    /**
     * @param context
     * @return
     */
    Bitmap selectArrow(Context context) {
        if (arrSelect_ == null) {
            arrSelect_ = ImageRepository.getImageFromAssetFile(context, "select_arr.png");
        }
        return arrSelect_;
    }

    /**
     * @param context
     * @return
     */
    public Bitmap selectedMenuBackground(Context context) {
        if (statusSelected_ == null) {
            statusSelected_ = ImageRepository.getImageFromAssetFile(context, "selectedmenubg.png");
        }
        return statusSelected_;
    }

    public static Bitmap getImageFromAssetFile(Context resources, String fileName) {
        Bitmap image = null;
        try {
            AssetManager am = resources.getAssets();
            InputStream is = am.open(fileName);
            image = BitmapFactory.decodeStream(is);
            is.close();
        } catch (Exception e) {

        }
        return image;
    }

    /**
     * @param imgOri
     * @param width
     * @return
     */
    public static Bitmap makeButton(Bitmap imgOri, int width) {
        int ori_w = imgOri.getWidth();
        int ori_h = imgOri.getHeight();
        int[] ori_rgb = new int[ori_w * ori_h];
        int[] dst_rgb = new int[width * ori_h];

        int dst = 0;
        int src = 0;
        imgOri.getPixels(ori_rgb, 0, ori_w, 0, 0, ori_w, ori_h);

        int middle = width - ori_w;
        // Copy the first 6 columns from the ori image to the left border.
        for (int i = 0; i < ori_h; ++i) {
            if (i > 0) {
                src = i * ori_w;
            }
            for (int j = 0; j < ori_w; ++j) {
                dst_rgb[dst++] = ori_rgb[src++];
            }
            src -= 1;
            // Copy background from the 6-th column of the source image.
            for (int j = ori_w; j < middle; ++j) {
                dst_rgb[dst++] = ori_rgb[src];
            }
            // Copy the reverse of ori image to the right border.
            for (int j = middle; j < width; ++j) {
                dst_rgb[dst++] = ori_rgb[src--];
            }
        }
        Bitmap imgDst = Bitmap.createBitmap(dst_rgb, width, ori_h, Bitmap.Config.ARGB_8888);
        return imgDst;
    }

    /**
     * @param imgArr
     * @param width
     * @param height
     * @return
     */
    public static Bitmap[] getArrowIcon(Bitmap imgArr, int width, int height) {
        Bitmap[] icon = null;
        Bitmap[] arrowIcons = createArrowIcon(imgArr);
        int size = arrowIcons != null ? arrowIcons.length : 0;
        if (size > 0) {
            icon = new Bitmap[size];
        }
        for (int i = 0; i < size; i++) {
            icon[i] = Bitmap.createScaledBitmap(arrowIcons[i], width, height, true);
        }
        return icon;
    }

    /**
     * @param imgArr
     * @return
     */
    private static Bitmap[] createArrowIcon(Bitmap imgArr) {
        Bitmap[] arrowIcons = null;
        try {
            // 原图未选中时的RGB
            int dataLeftOff[];
            // 原图选中时的RGB
            int dataLeftOn[];
            // 反转后未选中时的RGB
            int dataRightOff[];
            // 反转后选中时的RGB
            int dataRightOn[];

            Bitmap temp = imgArr;
            int eachWidth = temp.getWidth() / 2;
            int height = temp.getHeight();

            dataLeftOff = new int[eachWidth * height];
            dataLeftOn = new int[eachWidth * height];
            dataRightOff = new int[eachWidth * height];
            dataRightOn = new int[eachWidth * height];

            Bitmap leftOn = Bitmap.createBitmap(temp, 0, 0, eachWidth, height);
            Bitmap leftOff = Bitmap.createBitmap(temp, eachWidth, 0, eachWidth, height);
            leftOn.getPixels(dataLeftOn, 0, eachWidth, 0, 0, eachWidth, height);
            leftOff.getPixels(dataLeftOff, 0, eachWidth, 0, 0, eachWidth, height);
            // 像素移动的目的位置
            int destIndex;
            // 读取像素的位置
            int orIndex;
            for (int i = 0; i < height; i++) {
                // 单个像素对应的二维行位置
                int numIndex = i * eachWidth;

                for (int j = 0; j < eachWidth; j++) {
                    destIndex = numIndex + j;
                    orIndex = numIndex + eachWidth - j - 1;
                    dataRightOff[destIndex] = dataLeftOff[orIndex];
                    dataRightOn[destIndex] = dataLeftOn[orIndex];
                }
            }
            Bitmap rightOff = Bitmap.createBitmap(dataRightOff, eachWidth, height, Bitmap.Config.ARGB_8888);
            Bitmap rightOn = Bitmap.createBitmap(dataRightOn, eachWidth, height, Bitmap.Config.ARGB_8888);
            arrowIcons = new Bitmap[4];
            arrowIcons[0] = leftOn;
            arrowIcons[1] = leftOff;
            arrowIcons[2] = rightOn;
            arrowIcons[3] = rightOff;
        } catch (Exception ex) {
            Utils.printException(ex);
        }

        return arrowIcons;
    }

    /**
     * @param context
     * @return
     */
    public Bitmap tabBackground(Context context) {
        if (backgroundTab_ == null) {
            backgroundTab_ = ImageRepository.getImageFromAssetFile(context, "tabbarbg.png");
        }
        return backgroundTab_;
    }

    /**
     * @param context
     * @return
     */
    Bitmap tabArrow(Context context) {
        if (arrTab_ == null) {
            arrTab_ = ImageRepository.getImageFromAssetFile(context, "arrow.png");
        }
        return arrTab_;
    }

    /**
     * NOTE: the source bitmap may be recycle.
     * 
     * @param src
     *            source image.
     * @param destWidth
     *            expected width
     * @param keepScale
     *            keeps the scale of the width and height
     * @param isBackgroundTrans
     *            makes the background transparent
     * @param isTrans
     *            makes the image transparent
     * @return Bitmap
     */
    public static final Bitmap scaleImage(final Bitmap src, final int destWidth, final boolean keepScale,
            final boolean isBackgroundTrans, final boolean isTrans) {
        Bitmap destImg = src;
        try {
            int orgWidth = src.getWidth();
            int orgHeight = src.getHeight();
            if (destWidth == orgWidth) {
                return src;
            }
            float scaleWidth = ((float) destWidth) / orgWidth;
            float scaleHeight = 1.0f;
            if (keepScale) {
                scaleHeight = scaleWidth;
            }
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            destImg = Bitmap.createBitmap(src, 0, 0, orgWidth, orgHeight, matrix, true);
            recycleBitmap(src);
        } catch (Exception ex) {
            Utils.printException(ex);
            destImg = src;
        }
        return destImg;
    }

    /**
     * @param src
     *            source image.
     * @param keepScale
     *            keeps the scale of the width and height
     * @param desH
     *            expected height
     * @param isBackgroundTrans
     *            makes the background transparent
     * @param isTrans
     *            makes the image transparent
     * @return Image
     */
    public static final Bitmap scaleImage(Bitmap src, boolean keepScale, int desH, boolean isBackgroundTrans,
            boolean isTrans) {
        Bitmap destImg = null;
        try {
            int desW = src.getWidth();
            if (keepScale) {
                desW = desW * desH / src.getHeight();
            }
            Matrix matrix = new Matrix();
            float scaleWidth = desW;
            float scaleHeight = desH;
            matrix.postScale(scaleWidth, scaleHeight);
            destImg = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
        } catch (Exception ex) {

            Utils.printException(ex);
            destImg = null;
        }
        return destImg;
    }

    /**
     * @param bitmap
     */
    public static final void recycleBitmap(Bitmap bitmap) {
        if (bitmap == null) {
            return;
        }
        if (!bitmap.isRecycled()) {
            bitmap.recycle();
        }
    }

}
